package com.anxin.act.task.service;

import com.anxin.act.task.dao.ViewHiTaskDao;
import com.anxin.act.task.dao.ViewRuTaskDao;
import com.anxin.act.task.dao.WfPushLogDao;
import com.anxin.act.task.entity.ViewHiTask;
import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.task.entity.WfPushLog;
import com.anxin.common.utils.DateUtils;
import com.anxin.common.utils.LogUtils;
import com.anxin.common.utils.SecurityUtils;
import com.anxin.common.utils.StringUtils;
import com.anxin.common.utils.sys.UserUtils;
import com.anxin.framework.aspectj.lang.annotation.Log;
import com.anxin.framework.aspectj.lang.enums.BusinessType;
import com.anxin.framework.web.entity.AjaxResult;
import com.anxin.framework.web.service.BaseService;
import com.anxin.sys.system.entity.SysUser;
//import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * 流程任务推送记录Service业务层处理
 *
 * @author jbs
 * @date 2022-04-13
 */
@Service
public class WfPushLogService extends BaseService<WfPushLogDao, WfPushLog>{
    @Autowired
    private WfPushLogDao wfPushLogDao;
    @Autowired
    private ActDockingSendService actDockingSendService;
    @Autowired
    private ViewRuTaskDao viewRuTaskDao;
    @Autowired
    private ViewHiTaskDao viewHiTaskDao;

    /**
     * 查询流程任务推送记录
     *
     * @param id 流程任务推送记录ID
     * @return 流程任务推送记录
     */
    public WfPushLog getById(String id){
        return wfPushLogDao.getById(id);
    }

    /**
     * 查询流程任务推送记录列表
     *
     * @param wfPushLog 流程任务推送记录
     * @return 流程任务推送记录
     */
    public List<WfPushLog> findList(WfPushLog wfPushLog){
        return wfPushLogDao.findList(wfPushLog);
    }

    /**
     * 根据taskId查询推送过的任务
     * @param taskId 任务ID
     * @return
     */
    public List<WfPushLog> findListByTaskId(String taskId,String pushType){
        return wfPushLogDao.findListByTaskId(taskId,pushType);
    }


    /**
     * 根据taskId查询推送过的任务
     * @param taskId 任务ID
     * @return
     */
    public List<WfPushLog> findListByActOwner(String taskId,String actOwner){
        return wfPushLogDao.findListByActOwner(taskId,actOwner);
    }

    /**
     * 根据actId查询推送过的任务
     * @param actId 任务ID
     * @return
     */
    public List<WfPushLog> findListByActId(String actId){
        return wfPushLogDao.findListByActId(actId);
    }


    /**
     * 根据taskId查询是否发送过待办提醒
     * @param taskId taskId
     * @return
     */
    public Integer getTodoCountByTaskId(String taskId,String actOwner){
        return wfPushLogDao.getTodoCountByTaskId(taskId,actOwner);
    }


    /**
     * 保存流程任务推送记录
     *
     * @param wfPushLog 流程任务推送记录
     * @return 结果
     */
    public int save(WfPushLog wfPushLog){
        return super.save(wfPushLog);
    }


    /**
     * 删除流程任务推送记录信息
     *
     * @param id 流程任务推送记录ID
     * @return 结果
     */
    public int deleteById(String id){
        SysUser user = SecurityUtils.getUser();
        return wfPushLogDao.deleteById(id,user.getId());
    }

    /**
     * 发送待办提醒
     * @param viewRuTask
     */
    @Transactional(readOnly = false)
    public void sendTodoTask(ViewRuTask viewRuTask, String actOwner, String actAssignee, String status, String message) {
        WfPushLog wfPushLog = new WfPushLog(viewRuTask);
        wfPushLog.setActOwner(actOwner);
        wfPushLog.setActAssignee(actAssignee);
        wfPushLog.setActTitle(viewRuTask.getTitle());
        wfPushLog.setStatus(status);
        wfPushLog.setMessage(message);
        this.save(wfPushLog);
        LogUtils.info("② 成功保存推送的待办信息");

    }

    /**
     * 发送待办提醒
     * @param viewHiTask
     */
    @Transactional(readOnly = false)
    public void sendHiTask(ViewHiTask viewHiTask, String userId, String status, String message){
        WfPushLog wfPushLog = new WfPushLog(viewHiTask);
        wfPushLog.setActTitle(viewHiTask.getTitle());
        wfPushLog.setActOwner(userId);
        wfPushLog.setActAssignee(userId);
        wfPushLog.setStatus(status);
        wfPushLog.setMessage(message);
        this.save(wfPushLog);
    }

    /**
     * 待办任务重新推送
     * @param id ID
     * @return
     */
    @Transactional(readOnly = false)
    public AjaxResult againPush(String id){
        WfPushLog wfPushLog = wfPushLogDao.getById(id);
        // 重新推送待办
        if ("TODO".equals(wfPushLog.getPushType())){
            ViewRuTask viewRuTask = viewRuTaskDao.getById(wfPushLog.getTaskId());
            if (viewRuTask != null){
                wfPushLogDao.updateStatus(id);
                SysUser user = UserUtils.getUser(wfPushLog.getActAssignee());
                actDockingSendService.sendTodoTask(viewRuTask,user.getId(),user.getUserName());
            }else {
                return AjaxResult.error("推送失败,待办任务不存在");
            }
        }else {
            ViewHiTask viewHiTask = viewHiTaskDao.getById(wfPushLog.getTaskId());
            if (viewHiTask != null){
                wfPushLogDao.updateStatus(id);
                SysUser user = UserUtils.getUser(wfPushLog.getActAssignee());
                actDockingSendService.sendHiTask(viewHiTask,user.getId(),user.getUserName());
            }else {
                return AjaxResult.error("推送失败,已办任务不存在");
            }
        }

        return AjaxResult.success();

    }

    /**
     * 删除待办推送
     * @param id ID
     * @return
     */
    @Transactional(readOnly = false)
    public void delTodo(String id){
        WfPushLog wfPushLog = wfPushLogDao.getById(id);
        SysUser user = UserUtils.getUser(wfPushLog.getActAssignee());
        wfPushLogDao.deleteById(id);
        if (StringUtils.equals ("JD", wfPushLog.getPushType ())) {
            actDockingSendService.delTodoTask (wfPushLog.getActId (), wfPushLog.getActId () + user.getUserName (), user.getId (), user.getUserName ());
        } else {
            actDockingSendService.delTodoTask (wfPushLog.getActId (), wfPushLog.getTaskId (), user.getId (), user.getUserName ());
        }
    }

    /**
     * 删除已办
     * @param viewHiTask
     * @param user
     */
    public void delHiTask(ViewHiTask viewHiTask,SysUser user){
        wfPushLogDao.delHiTask(viewHiTask.getId(),user.getId());
        actDockingSendService.delHiTask(viewHiTask,user.getId(),user.getUserName());
    }

    /**
     * 初始化数据
     */
    public void initData(){
        // 删除待办
        ViewHiTask viewHiTask = new ViewHiTask();
        viewHiTask.setApprovalTime(DateUtils.dateTime("yyyy-MM-dd HH:mm:ss",""));
        List<ViewHiTask> viewHiTaskList = viewHiTaskDao.findList(viewHiTask);
        for (ViewHiTask hiTask : viewHiTaskList) {
            WfPushLog wfPushLog = new WfPushLog();
            wfPushLog.setActId(hiTask.getActId());
            wfPushLog.setTaskId(hiTask.getId());
            wfPushLog.setActAssignee(hiTask.getAssignee());
            wfPushLog.setStatus("SUCCESS");
            wfPushLog.setPushType("HI");
            List<WfPushLog> wfPushLogList = this.findList(wfPushLog);
            if (wfPushLogList.size() == 0){
                // 删待办
                // 发已办
            }

        }

        // 没有发待办的发送待办
        ViewRuTask viewRuTask = new ViewRuTask();
        viewRuTask.setHang("N");
        List<ViewRuTask> viewRuTaskList = viewRuTaskDao.findList(viewRuTask);
        for (ViewRuTask ruTask : viewRuTaskList) {
            WfPushLog wfPushLog = new WfPushLog();
            wfPushLog.setActId(ruTask.getActId());
            wfPushLog.setTaskId(ruTask.getId());
            wfPushLog.setActAssignee(ruTask.getAssignee());
            wfPushLog.setStatus("SUCCESS");
            wfPushLog.setPushType("TODO");
            List<WfPushLog> wfPushLogList = this.findList(wfPushLog);
            if (wfPushLogList.size() == 0){
                // 发待办
            }
        }
    }

    public WfPushLog findByTaskId(String taskId, String pushType) {
        return wfPushLogDao.findByTaskId(taskId,pushType);
    }

    public List<WfPushLog> findByActId(String refId, String userId) {
        return wfPushLogDao.findByActId(refId, userId);
    }

    public String selectTaskId(String actId, String userId) {
        return wfPushLogDao.selectTaskId(actId,userId);
    }

    public WfPushLog selectWfPushLog(WfPushLog wfPushLog) {
        return wfPushLogDao.selectWfPushLog(wfPushLog);
    }
}
